package com.tn.bo;
import java.io.StringWriter;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
public class XMLUtil {
	
	/*public static void printAsXML(Object obj) throws Exception{
        Class c = obj.getClass();
        String cname = c.getName();
        int i = cname.lastIndexOf('.');
        cname = cname.substring(i+1);
        System.out.println("<" + cname + ">");
        printFields(c, obj);
        System.out.println("</" + cname + ">");
    }

    private static void printFields(Class c, Object obj) throws Exception{
        Class sc = c.getSuperclass();
        if(sc != Object.class ) {
        		printFields(sc, obj);
        }   	
        Field[] f = c.getDeclaredFields();
        for(int i = 0; i < f.length; i++){
            int m = f[i].getModifiers();
            
            //System.out.print(f[i].getType().getName());
            if(Modifier.isStatic(m) || Modifier.isTransient(m)) continue;
            String fname = f[i].getName();            
            f[i].setAccessible(true);
            System.out.print("    <" + fname + ">"); 
            System.out.print(f[i].get(obj));           
            System.out.println("</" + fname + ">");            
        }
    }*/
    
    public static String getPurchaseOrderXml(POHeader poHeader){
    	XStream xStream = new XStream(); 
    	xStream.alias("PurchaseOrder",POHeader.class);
    	xStream.alias("PurchaseOrderLineItem",POLineItem.class);
    	xStream.setMode(XStream.NO_REFERENCES);
		StringWriter s = new StringWriter();
		PrettyPrintWriter p = new PrettyPrintWriter(s);
		xStream.marshal(poHeader, p);
		return s.toString();
    }
    
    public static String getInvoiceXml(Invoice invoice){
    	XStream xStream = new XStream(); 
    	xStream.alias("Invoice",Invoice.class);
    	xStream.alias("InvoiceLineItem",InvoiceLineItem.class);
    	xStream.setMode(XStream.NO_REFERENCES);
		StringWriter s = new StringWriter();
		PrettyPrintWriter p = new PrettyPrintWriter(s);
		xStream.marshal(invoice, p);
		return s.toString();
    }
    
    public static String getPaymentXml(Payment payment){
    	XStream xStream = new XStream(); 
    	xStream.alias("Payment",Payment.class);
    	xStream.setMode(XStream.NO_REFERENCES);
		StringWriter s = new StringWriter();
		PrettyPrintWriter p = new PrettyPrintWriter(s);
		xStream.marshal(payment, p);
		return s.toString();
    }
    
    public static String getCreditMemoXml(CreditMemo creditMemo){
    	XStream xStream = new XStream(); 
    	xStream.alias("CreditMemo",CreditMemo.class);
    	xStream.alias("CreditMemoLineItem",CreditMemoLineItem.class);
    	xStream.setMode(XStream.NO_REFERENCES);
		StringWriter s = new StringWriter();
		PrettyPrintWriter p = new PrettyPrintWriter(s);
		xStream.marshal(creditMemo, p);
		return s.toString();
    }

}
